Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

buffer-layout

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

buffer-layout

Translation between JavaScript values and Buffers

  • 1.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
137K
increased by13.47%
Maintainers
1
Weekly downloads
 
Created

What is buffer-layout?

The buffer-layout npm package is a library for creating and parsing binary data layouts in JavaScript. It is particularly useful for working with binary data structures, such as those used in network protocols or file formats, by providing a way to define and manipulate complex data layouts in a structured manner.

What are buffer-layout's main functionalities?

Defining a Structure

This feature allows you to define a structured layout for binary data. In this example, a structure for a 'person' is defined with fields for age, height, and name. The structure is then encoded into a buffer and decoded back into a JavaScript object.

const BufferLayout = require('buffer-layout');

const personLayout = BufferLayout.struct([
  BufferLayout.u8('age'),
  BufferLayout.u32('height'),
  BufferLayout.cstr('name')
]);

const buffer = Buffer.alloc(100);
personLayout.encode({ age: 25, height: 180, name: 'Alice' }, buffer);
const person = personLayout.decode(buffer);
console.log(person);

Nested Structures

This feature demonstrates how to define nested structures within a layout. The example shows a 'person' structure that includes an 'address' structure, allowing for more complex data representations.

const BufferLayout = require('buffer-layout');

const addressLayout = BufferLayout.struct([
  BufferLayout.u8('houseNumber'),
  BufferLayout.cstr('street')
]);

const personLayout = BufferLayout.struct([
  BufferLayout.u8('age'),
  BufferLayout.u32('height'),
  BufferLayout.cstr('name'),
  BufferLayout.struct(addressLayout, 'address')
]);

const buffer = Buffer.alloc(100);
personLayout.encode({ age: 30, height: 175, name: 'Bob', address: { houseNumber: 123, street: 'Main St' } }, buffer);
const person = personLayout.decode(buffer);
console.log(person);

Variable Length Fields

This feature allows for defining fields with variable lengths, such as strings or blobs of data. The example shows a 'message' structure with a 'data' field whose length is determined by another field in the structure.

const BufferLayout = require('buffer-layout');

const messageLayout = BufferLayout.struct([
  BufferLayout.u8('type'),
  BufferLayout.u32('length'),
  BufferLayout.blob(BufferLayout.offset(BufferLayout.u32(), -4), 'data')
]);

const buffer = Buffer.alloc(100);
messageLayout.encode({ type: 1, length: 5, data: Buffer.from('Hello') }, buffer);
const message = messageLayout.decode(buffer);
console.log(message);

Other packages similar to buffer-layout

Keywords

FAQs

Package last updated on 06 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc